//============================================================================
// Copyright (C) 2010 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================
enum EPlayStateType 
{
	ePlayStateNone			= 0, // not initialized
	ePlayStatePlay			= 1, 
	ePlayStatePaused		= 2, 
//	ePlayStateWin			= 3, 
//	ePlayStateLose			= 4, 
//	ePlayStateHighScore		= 5, 
	eMaxPlayStateType, // must be last
};

#define Y_AXIS_ORIENTATION_MAX_SAMPLES 6

class DtwStatePlay : public GameStateBase
{
public:
	//=== vars ===//
	DtwGameMgr *					m_poGameMgr;			// game manager
	EPlayStateType					m_eCurPlayState;		// current state
	EPlayStateType					m_eTargetPlayState;		// next state if any

	//=== time vars ===//
	U32								m_u32AppTimeLastUpdateMs;		// time of last update in millisec since app started
	U32								m_u32MsElapsedThisFrame;		// milliseconds elapsed this frame ( time in millisec since last update )
	int 							m_iComet1Seconds;
	int 							m_iComet2Seconds;
	int 							m_iAsteroidRedSeconds;
	int 							m_iAsteroidBlueSeconds;
	int 							m_iCheckIfEarthDestroyedSeconds;
	U32 							m_u32LastSysTimeSec;

	//=== game state vars ===//
	BOOL							m_bRunning;
	F32								m_f32WorldScale;

	//=== game scene vars ===//
	irr::scene::VxProgressBarNode *	m_poProgressBar;

	// earth
	EntEarth *						m_poEntEarth;
//	irr::scene::IAnimatedMesh* 		m_poEarthMesh; 			// pointer to earth mesh
//	irr::scene::ISceneNode* 		m_poEarthNode;			// pointer to earth node
	irr::core::vector3df			m_v3fEarthPos;			// position of earth in world coords
	float							m_f32EarthSize;			// diameter of earth
	// Comet Fire
	EntCometFire *					m_poEntCometRed;
	irr::core::vector3df			m_v3fCometRedStartPos;	// starting position of comet 1 starting pos in world coords
	float							m_f32CometRedSize;		// diameter of comet 1
	// Comet Ice
	EntCometIce *					m_poEntCometBlue;
	irr::core::vector3df			m_v3fCometBlueStartPos;	// starting position of comet 2 starting pos in world coords
	float							m_f32CometBlueSize;		// diameter of comet 2
	// asteroid red
	EntAsteroidRed *				m_poEntAsteroidRed;
	irr::core::vector3df			m_v3fAsteroidRedStartPos;		// starting position of asteroid red starting pos in world coords
	int								m_iAsteroidRedFireInterval;		// time in seconds asteroid must be in start position before firing
	float							m_f32AsteroidRedVelocity;		// velocity to fire asteroid at
	// asteroid blue
	EntAsteroidBlue *				m_poEntAsteroidBlue;
	irr::core::vector3df			m_v3fAsteroidBlueStartPos;		// starting position of asteroid blue starting pos in world coords
	int								m_iAsteroidBlueFireInterval;	// time in seconds asteroid must be in start position before firing
	float							m_f32AsteroidBlueVelocity;		// velocity to fire asteroid at
	//=== world limits vars ===//
	irr::core::aabbox3df			m_oWorldBBox;			// bounding box of world
	irr::core::vector3df			m_v3fPlusZExtreme;		// maximum +z pos of world in x-z plane
	irr::core::vector3df			m_v3fMinusZExtreme;		// maximum -z pos of world in x-z plane
	irr::core::vector3df			m_v3fPlusXExtreme;		// maximum +x pos of world in x-z plane
	irr::core::vector3df			m_v3fMinusXExtreme;		// maximum -x pos of world in x-z plane
	irr::core::vector3df			m_v3fPlusZMin;			// min +z visible position in world in x-z plane
	irr::core::vector3df			m_v3fMinusZMin;			// min -z visible position in world in x-z plane
	irr::gui::IGUIStaticText *		m_poTextFps;			// frames per second
	irr::gui::IGUIStaticText *		m_poTextGameTime;		// amount of time in game level
	
	//=== touch event vars ===//
	U32								m_u32TouchStartMs;		// start time in ms of touch
	F32								m_f32TouchDnX;			// x position of touch down event
	F32								m_f32TouchDnY;			// y position touch down event start 
	F32								m_f32TouchUpX; 			// x position of touch up event
	F32								m_f32TouchUpY; 			// y position of touch up event
	BOOL							m_bIsLongPress;			// if true is a long press
	F32								m_f32ScaledTouchSlopSquared; // touch distance slop based on screen size
	F32								m_bTouchHasNotMoved; 	// if true touch was not moved from start ( except within slop distance )
	irr::scene::ISceneNode* 		m_poTouchSceneNode;		// scene node touched during touch down event

	//=== average orientation for less jitter ===//
	float 		m_af32YAxisOrientationAvg[ Y_AXIS_ORIENTATION_MAX_SAMPLES ];
	int 		m_iYAxisAvgIdx;



	//=== constructor ===//
	DtwStatePlay();
	//=== destructor ===//
	~DtwStatePlay();

	//=== state overrides ===//
	//! startup
	virtual RCODE	GameStateStartup( VxIrrBase * poIrrBase );
	//! shutdown
	virtual void	GameStateShutdown( void );
	//! update current game state.. time is millisec since app started
	virtual RCODE	GameStateUpdate( U32 u32AppTimeMs );
	//! called when application is about to sleep
	virtual RCODE	GameStateAppSleep( void );
	//! called when application is resuming from sleep.. app must reload textures etc
	virtual RCODE	GameStateAppResume( U32 u32FrameTimeMs );
	//! interpolate before draw
	virtual void	InterpolatePositions( F32 f32TimeMs );
	//! draw scene
	virtual void	Draw( void );

	//=== input handlers ===//
	//! handle orientation events
	virtual bool	OnOrientationEvent( F32 f32RotX, F32 f32RotY, F32 f32RotZ  );
	//! handle mouse events
	virtual bool	OnMouseEvent( EMouseButtonType eMouseButType, EMouseEventType eMouseEventType, int iMouseXPos, int iMouseYPos  );

	//=== properties ===//

	//=== methods ===//
	//! find scene node touched from cursor position ( NULL if no collision )
	irr::scene::ISceneNode * TouchedSceneNode(	irr::core::position2d<irr::s32> pos,// cursor position
												irr::s32 s32Range,					// check at four corners at this distance from cursor position
												irr::s32 s32ObjIdMask );			// object id mask when checking for picked object
	//! determine dimensions of game world etc
	void			CalculateWorldLimits( void );
	//! place object positions etc
	void			DetermineGeometry( void );
	//! game object touched then released.. determine what to do
	void			GameObjToss( void );

	//! earth is destroyed.. play animations etc
	void			DoDestroyEarth( void );
	//! load or reload resources
	//! bReloadFromResume = true if reloading after application resume
	RCODE			LoadResources( BOOL bReloadFromResume  );
	//! update percent of earth destroyed
	void			UpdateProgress( int iPercentDestroyed );

	//! average the orientation y axis value so world tilt is less jittery
	float 			GetYAxisAverage( float f32RotY );
	//! do level completed action
	void			GameLevelCompleted( void );

	//! fire asteroid in front of earth
	void			FireRedAsteroid( void );
	//! fire asteroid in front of earth
	void			FireBlueAsteroid( void );
};



